<?php

	/**
	 * Workspace@ Engine application comment!
	 * 
	 * LICENSE
	 * This application is licensed under New BSD license.
	 * Please read complete LICENSE.txt inside of root folder for more details.
	 * 
	 * We wish you good building :) 
	 * 
	 * @category   Application
	 * @package    Bootstrap
	 * @author     Nevio Vesic <nevio@workspaceat.com>
	 * @copyright  2008-2009 Workspace@ group in association with Design strategist
	 */

	/**
	 * @Zend_Session_Namespace
	 */
	require_once 'Zend/Session/Namespace.php';

	/**
	 * Enter description here...
	 *
	 */
	class Engine_Application_Configuration extends Engine_Application_Locator 
	{
		/**
		 * Base path to application configuration directory
		 */
		const BASE_PATH     = "library/Application/Configuration/";
		
		/**
		 * Base path to application configuration directory
		 */
		const CONF_BASE_PATH     = "";
		
		/**
		 * PHP file extension
		 */
		const FILE_EXTENSION = ".php";
		
		/**
		 * Current object instance
		 *
		 * @var Object[Engine_Application_Configuration_*]
		 */
		protected static $config  = null;
		
		/**
		 * Current enviroment ( backend or frontend )
		 *
		 * @var string
		 */
		protected static $enviroment = null;
		
		/**
		 * Application name... Used for .config/ folder and resolving which config folder to load!
		 *
		 * @var string
		 */
		protected static $appname    = null;
		
		/**
		 * Configuration class prefix
		 */
		const CONFIGCLASS_PREFIX     = "Engine_Application_Configuration_";
	
		/**
		 * Database file constant
		 */
		const DATABASE_FILE          = "database.php";
		
		/**
		 * Load configuration factory instance
		 *
		 * @return Object[Engine_Application_Configuration_*]
		 */
		public static function factory( $appname )
		{
			if ( self::$enviroment )
			{
				self::$enviroment = null;
			}
			
			if ( self::$appname )
			{
				self::$appname = null;
			}
			
			self::$appname = $appname;
			
			self::$enviroment = ucfirst ( parent::getApplicationEnviroment() );
			
			if (! @fopen( self::BASE_PATH . self::$enviroment . self::FILE_EXTENSION, "r", true ) )
			{
				throw new Engine_Exception ( "Configuration file for current enviroment cannot be found! Please verify configuration!" );
			}
			
			require_once self::BASE_PATH . self::$enviroment . self::FILE_EXTENSION;
			
			if (! class_exists( self::CONFIGCLASS_PREFIX . self::$enviroment ) )
			{
				throw new Engine_Exception ( "Configuration object for current enviroment cannot be found! Please verify configuration!" );
			}
			
			if ( self::$config )
			{
				self::$config = null;
			}
			
			$env_class = self::CONFIGCLASS_PREFIX . self::$enviroment;
			
			self::$config = new $env_class( $appname );
			
			return self::$config;
			
			
		}
		
		public static function loadDatabase()
		{
			return self::CONF_BASE_PATH . strtolower ( self::$appname ) . DIRECTORY_SEPARATOR . self::DATABASE_FILE;
		}
		
		/**
		 * Return database instance
		 *
		 * @return Object[Zend_Db]
		 */
		public static function databaseInstance()
		{
			return Zend_Registry::get("database");
		}

	}